import mysql.connector
from lianjia import settings

MYSQL_HOSTS = settings.MYSQL_HOSTS
MYSQL_USER = settings.MYSQL_USER
MYSQL_PASSWORD = settings.MYSQL_PASSWORD
MYSQL_PORT = settings.MYSQL_PORT
MYSQL_DB = settings.MYSQL_DB

cnx = mysql.connector.connect(user=MYSQL_USER, password=MYSQL_PASSWORD, host=MYSQL_HOSTS, database=MYSQL_DB)
cur = cnx.cursor(buffered=True)


class Sql:

    @classmethod
    def insert_linked(cls, pic, name, city, meters, sizeDes, village, rent, direction):
        #
        sql = "INSERT INTO db_linked ( `pic`,`name`,`city`,`meters`,`size_des`,`village`,`rent`,`direction`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"

        value = {
            'pic': pic,
            'name': name,
            'city': city,
            'meters': meters,
            'sizeDes': sizeDes,
            'village': village,
            'rent': rent,
            'direction': direction
        }
        try:
            cur.execute(sql, (pic, name, city, meters, sizeDes, village, rent, direction))
            cnx.commit()
        except Exception as e:
            print(Exception, ":", e)
